package sf.hmg.turntest;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class turntest extends Activity {
	/** Called when the activity is first created. */
	private PageWidget mPageWidget;
	Bitmap mCurPageBitmap, mNextPageBitmap;
	Canvas mCurPageCanvas, mNextPageCanvas;
	IPageFactory pagefactory;
	private float xStart,yStart;
	private boolean IsMove;
	
	private LayoutInflater inflater;
	private FrameLayout bigPicLayout;
	private ImageView bigPicImageView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mPageWidget = new PageWidget(this);
		setContentView(mPageWidget);
		
//		inflater=LayoutInflater.from(this);
//		bigPicLayout = (FrameLayout) inflater.inflate(R.layout.main, null).findViewById(R.id.BigPicFrameLayout);
//		bigPicImageView = (ImageView) inflater.inflate(R.layout.main, null).findViewById(R.id.BigPicImageView);
//
//		bigPicLayout.setId(1);
//		bigPicImageView.setId(2);
//		bigPicLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		//bigPicImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		
//		mPageWidget.addView(bigPicLayout);//, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//		mPageWidget.addView(bigPicImageView);//, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		bigPicLayout = new FrameLayout(this);
		bigPicLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		mPageWidget.addView(bigPicLayout);
		bigPicImageView = new ImageView(this);
		bigPicImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		
		bigPicLayout.addView(bigPicImageView);
		
//		VideoView vv = new VideoView(this);
//		vv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//		vv.setVideoURI(Uri.parse("file:///sdcard/Video/Camera Video/100615A000.mp4"));
//		MediaController mc = new MediaController(this);
//		vv.setMediaController(mc);
//		mPageWidget.addView(vv);
		
//		SurfaceView surfaceView = new SurfaceView(this);
//		SurfaceHolder surfaceHolder = surfaceView.getHolder();
//		surfaceHolder.setFixedSize(100, 100);
//		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//		
//		
//		MediaPlayer mediaPlayer = new MediaPlayer();
//		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//		mediaPlayer.setDisplay(surfaceHolder);
//		try
//		{
//		    mediaPlayer.setDataSource("file:///sdcard/Video/Camera Video/100615A000.mp4");
//		    mediaPlayer.prepare();
//		    mediaPlayer.start();
//		}
//		catch (Exception e)
//		{
//		}
//		
//		surfaceView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//		mPageWidget.addView(surfaceView);
		
//		TextView tv = new TextView(this);
//		tv.setText("FUCKdddddddddddddddd");
//		tv.setTextColor(Color.RED);
//		tv.setId(1);
//		tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//		mPageWidget.addView(tv);
		

		mCurPageBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
		mNextPageBitmap = Bitmap
				.createBitmap(480, 800, Bitmap.Config.ARGB_8888);

		mCurPageCanvas = new Canvas(mCurPageBitmap);
		mNextPageCanvas = new Canvas(mNextPageBitmap);
		pagefactory = new PhotoPageFactory(480, 800, this);

		pagefactory.setBgBitmap(BitmapFactory.decodeResource(
				this.getResources(), R.drawable.bg2));

		try {
			pagefactory.openFile("/sdcard/test.txt");
			pagefactory.onDraw(mCurPageCanvas);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			Toast.makeText(this, "�����鲻����,�뽫��test.txt������SD����Ŀ¼��",
					Toast.LENGTH_SHORT).show();
		}

		mPageWidget.setBitmaps(mCurPageBitmap, mCurPageBitmap);

		mPageWidget.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent e) {
				
				boolean ret=false;
				if (v == mPageWidget) {
					if (e.getAction() == MotionEvent.ACTION_DOWN) {
						mPageWidget.abortAnimation();
						xStart = e.getX();
						yStart = e.getY();
						mPageWidget.calcCornerXY(e.getX(), e.getY());
						IsMove = false;
					}
					if(IsMove == false && e.getAction() == MotionEvent.ACTION_UP) {
						//pagefactory.onClick(e.getX(), e.getY());
						File file = new File(pagefactory.getCurrentPic());
						bigPicImageView.setImageURI(Uri.fromFile(file));
						Animation an = AnimationUtils.loadAnimation(turntest.this, R.anim.bigpic_appear);
						bigPicLayout.startAnimation(an);
						return true;
					}
					if (getDistance(xStart, yStart, e.getX(), e.getY()) > 200){
						if(IsMove == false){
							IsMove = true;
							pagefactory.onDraw(mCurPageCanvas);
							if (mPageWidget.DragToRight()) {
								if(pagefactory.isFirstPage()){
									IsMove = false;
									return false;
								}
								try {
									pagefactory.prePage();
								} catch (IOException e1) {
									e1.printStackTrace();
								}						
								pagefactory.onDraw(mNextPageCanvas);
							} else {
								if(pagefactory.isLastPage()){
									IsMove = false;
									return false;
								}
								try {
									pagefactory.nextPage();
								} catch (IOException e1) {
									e1.printStackTrace();
								}
								pagefactory.onDraw(mNextPageCanvas);
							}
							mPageWidget.setBitmaps(mCurPageBitmap, mNextPageBitmap);
						}else{
							ret = mPageWidget.doTouchEvent(e);
							return ret;
						}
					}
				}
				return true;
			}

		});
	}

	private float getDistance(float x, float y, float x2, float y2){
		float temp = (x-x2) * (x-x2) + (y - y2) * (y - y2);
		Log.i("Distnce", String.valueOf(temp));
		return temp;
	}
}